package com.photography.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.photography.mapper.EmployeeMapper;
import com.photography.pojo.Employee;
import com.photography.service.EmployeeService;
import com.photography.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.Date;
import java.util.List;
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IdWorker idWorker;
//
//    @Override
//    public PageResult<Employee> findAll(int page,int rows) {
//        Page<Employee> employeePage = new Page<>(page, rows);
//        EntityWrapper<Employee> wrapper = new EntityWrapper<>();
//        wrapper.eq("status",1).or().eq("status",0);
//        List<Employee> employees = employeeMapper.selectPage(employeePage, wrapper);
//        employeePage.setRecords(employees);
//        PageResult<Employee> employeePageResult = new PageResult<>();
//        employeePageResult.setTotal(employeePage.getTotal());
//        employeePageResult.setRows(employeePage.getRecords());
//        return employeePageResult;
//    }

    @Override
    public Employee findById(String id) {
        return employeeMapper.selectById(id);
    }

    @Override
    public void addEmployee(Employee employee) {
        employee.setId(idWorker.nextId()+"");
        employee.setCreated(new Date());
        employee.setUpdated(employee.getCreated());
        employeeMapper.insert(employee);
    }

    @Override
    public void updateStatusEmployee(String[] ids, int status) {
       for(String id : ids){
           Employee employee = employeeMapper.selectById(id);
           if (status == 0){
               employee.setStatus(0);
           }else if (status == 1){
               employee.setStatus(1);
           }else if (status == 2){
               employee.setStatus(2);
           }
           employeeMapper.updateById(employee);
       }
    }


    @Override
    public void updateEmployee(Employee employee) {
        employee.setUpdated(new Date());
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("id",employee.getId());
        employeeMapper.update(employee,wrapper);
    }
}
